<template>
  <div>
    <el-button type="text" icon="el-icon-circle-plus" size="mini" @click="openShop">添加</el-button>
    <!-- 弹框 -->
    <el-dialog
      v-dialogDrag
      ref="dialog"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="dialogVisible"
      :append-to-body="true"
      title="配件选择"
      width="70%"
      @close="clear">
      <!-- 查询条件 -->
      <el-form ref="searchForm" :model="search" label-width="95px" label-position="right" size="mini">
        <el-row :gutter="6">
          <el-col :xs="24" :sm="8" :md="6">
            <el-form-item prop="partCode" label="配件代码">
              <el-input v-model="search.partCode" clearable/>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="8" :md="6">
            <el-form-item prop="partName" label="配件名称">
              <el-input v-model="search.partName" clearable/>
            </el-form-item>
          </el-col>
          <el-col v-if="userInfo.dataType === 45081007" :xs="24" :sm="8" :md="6">
            <el-form-item prop="havingStock" label="有无库存">
              <el-checkbox v-model="search.havingStock">&nbsp;</el-checkbox>
            </el-form-item>
          </el-col>
          <div style="text-align:right">
            <el-button type="primary" icon="el-icon-search" @click="searchHandle">查询</el-button>
            <el-button icon="el-icon-refresh" @click="resetForm('searchForm')">重置</el-button>
          </div>
        </el-row>
      </el-form>
      <div class="table">
        <!--<div class="tableHead">
          <div class="title">配件信息</div>
        </div>-->
        <el-table v-loading="load" ref="table" :data="partList" tooltip-effect="dark" border >
          <el-table-column label="序号" type="index" min-width="40"/>
          <el-table-column label="操作" min-width="60">
            <template slot-scope="scope">
              <el-button
                :disabled="scope.row.PART_NO === undefined||scope.row.PART_NO === ''||scope.row.PART_NO === null"
                type="text"
                icon="el-icon-goods"
                @click="putData(scope.row)"/>
            </template>
          </el-table-column>
          <el-table-column prop="PART_NO" label="配件代码" min-width="150" show-overflow-tooltip/>
          <el-table-column prop="PART_NAME" label="配件名称" min-width="150" show-overflow-tooltip/>
          <el-table-column v-if="orderFormObj.isOem !== 10041001" prop="DLRSTOCK" label="账面库存" min-width="70"/>
          <el-table-column v-if="systemParam.COMPANY_CODE !== orderFormObj.saleOrgCode && orderFormObj.isOem === 10041001" prop="OEMSTOCK" label="账面库存" min-width="70"/>
          <el-table-column v-if="systemParam.COMPANY_CODE !== orderFormObj.saleOrgCode && orderFormObj.isOem !== 10041001" prop="STOCK_QUANTITY" label="上级库存" min-width="70">
            <template slot-scope="scope">
              <el-tag
                :type="scope.row.STOCK_QUANTITY === '无' ? 'primary' : 'success'"
                disable-transitions>{{ scope.row.STOCK_QUANTITY }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="UNIT" label="计量单位" min-width="80"/>
          <el-table-column prop="MIN_PACKAGE" label="最小包装数" min-width="90"/>
          <!--<el-table-column prop="MIN_ORDER_GOODS" label="最小订货量" min-width="90"/>-->
          <el-table-column :formatter="formatCurrency" prop="UNIT_PRICE" label="单价" min-width="100"/>
          <!--<el-table-column v-if="orderFormObj.orderType == 60041002" :formatter="formatCurrency" prop="URGENT_PRICE" label="紧急订货价" min-width="100"/>-->
          <el-table-column :formatter="filterCode" label="配件状态" type="1001" prop="PART_STATUS" min-width="100"/>
          <el-table-column prop="REMARK" label="备注" min-width="100" show-overflow-tooltip/>
        </el-table>
        <!-- 分页 -->
        <div style="text-align:right">
          <Pagination :data="page" @change-size="handleSizeChange" @change-page="handleCurrentChange"/>
        </div>
      </div>
      <br>
      <el-form ref="form" :model="form" label-width="95px" label-position="right" size="mini">
        <el-row :gutter="6">
          <el-col :xs="24" :sm="8" :md="6">
            <el-form-item prop="partCodeShow" label="配件代码">
              <el-input v-model="form.partCodeShow" disabled/>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="8" :md="6">
            <el-form-item prop="partNameShow" label="配件名称">
              <el-input v-model="form.partNameShow" disabled/>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="8" :md="6">
            <el-form-item prop="inQuantity" label="采购数量">
              <el-input v-model="form.inQuantity" clearable/>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="8" :md="6">
            <el-form-item prop="inPrice" label="单价">
              <el-input v-model="form.inPrice" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="table">
        <!--<div class="tableHead">
          <div class="title">替代件信息</div>
        </div>-->
        <BaseTableNoPage v-loading="optionLoad" ref="optionPartTable">
          <el-table-column label="序号" width="50" type="index"/>
          <el-table-column label="操作" min-width="60">
            <template slot-scope="scope">
              <el-button
                :disabled="scope.row.PART_NO === undefined||scope.row.PART_NO === ''||scope.row.PART_NO === null"
                type="text"
                icon="el-icon-sold-out"
                @click="optionPutData(scope.row)"/>
            </template>
          </el-table-column>
          <el-table-column prop="PART_NO" label="替代件代码" min-width="150" show-overflow-tooltip/>
          <el-table-column prop="PART_NAME" label="替代件名称" min-width="150" show-overflow-tooltip/>
          <el-table-column :formatter="filterCode" prop="OPTION_RELATION" label="替代类型" min-width="70" type="6007"/>
          <el-table-column v-if="orderFormObj.isOem !== 10041001" prop="DLRSTOCK" label="账面库存" min-width="70"/>
          <el-table-column v-if="systemParam.COMPANY_CODE !== orderFormObj.saleOrgCode && orderFormObj.isOem === 10041001" prop="OEMSTOCK" label="账面库存" min-width="70"/>
          <el-table-column v-if="systemParam.COMPANY_CODE !== orderFormObj.saleOrgCode && orderFormObj.isOem !== 10041001" prop="STOCK_QUANTITY" label="上级库存" min-width="70">
            <template slot-scope="scope">
              <el-tag
                :type="scope.row.STOCK_QUANTITY === '无' ? 'primary' : 'success'"
                disable-transitions>{{ scope.row.STOCK_QUANTITY }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="UNIT" label="计量单位" min-width="80"/>
          <el-table-column prop="MIN_PACKAGE" label="最小包装数" min-width="90"/>
          <!--<el-table-column prop="MIN_ORDER_GOODS" label="最小订货量" min-width="90"/>-->
          <el-table-column :formatter="formatCurrency" prop="UNIT_PRICE" label="单价" min-width="100"/>
          <!--<el-table-column v-if="orderFormObj.orderType == 60041002" :formatter="formatCurrency" prop="URGENT_PRICE" label="紧急订货价" min-width="100"/>-->
          <el-table-column prop="REMARK" label="备注" min-width="100" show-overflow-tooltip/>
        </BaseTableNoPage>
      </div>
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button type="text">
          <i class="el-icon-search magnifier" />
        </el-button>
        <el-button type="primary" @click="req">确 定</el-button>
        <el-button @click="clear">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getPartList, getPartOptionByPartNo
} from '@/api/part/dms/ordermanage/partOrder'
import Pagination from '@/components/Pagination'
import formMixins from '@/mixins/formMixins'
import { mapGetters } from 'vuex'
import rSelect from '@/components/Select/Select'
import BaseTable from '@/components/BaseTable'
import BaseTableNoPage from '@/components/BaseTable/BaseTableNoPage'
import _ from 'lodash'

export default {
  name: 'PartSelect',
  components: {
    Pagination,
    rSelect,
    BaseTable,
    BaseTableNoPage
  },
  mixins: [formMixins],
  props: {
    orderFormObj: Object
  },
  data() {
    return {
      partList: [],
      load: false,
      optionLoad: false,
      page: {},
      multipleSelection: [],
      search: {
        limit: 5,
        pageNum: 1,
        partCode: '',
        partName: '',
        havingStock: '',
        orderType: '',
        dealerCode: '',
        saleOrgCode: '',
        priceType: ''
      },
      form: {
        partCodeShow: '',
        partNameShow: '',
        inQuantity: '',
        inPrice: 0,
        inAmount: ''
      },
      dialogVisible: false
    }
  },
  computed: {
    ...mapGetters(['userInfo', 'systemParam'])
  },
  mounted() {
  },
  methods: {
    // 查询代理商选择列表
    openShop() {
      if (this.orderFormObj != null) {
        debugger
        if (this.orderFormObj.orderType === undefined || this.orderFormObj.orderType === '') {
          this.$message({
            type: 'info',
            message: '请选择订单类型!'
          })
          return
        } else {
          this.search.orderType = this.orderFormObj.orderType
        }
        if (this.orderFormObj.saleOrgCode === undefined || this.orderFormObj.saleOrgCode === '') {
          this.$message({
            type: 'info',
            message: '请选择供货单位!'
          })
          return
        } else {
          this.search.saleOrgCode = this.orderFormObj.saleOrgCode
        }
        if (this.orderFormObj.dealerCode === undefined || this.orderFormObj.dealerCode === '') {
          this.$message({
            type: 'info',
            message: '请选择订货单位!'
          })
          return
        } else {
          this.search.dealerCode = this.orderFormObj.dealerCode
        }
      //  this.search.priceType = this.orderFormObj.priceType
      }
      this.dialogVisible = true
      this.searchHandle()
    },
    // profitQuantityChange() {
    //   const num = Number(this.form.inPrice * this.form.inQuantity)
    //   this.form.inAmount = Number(num.toFixed(2))
    // },
    searchHandle() {
      this.load = true
      getPartList(this.search)
        .then(res => {
          this.partList = res.rows
          this.page = res
          this.load = false
        })
        .catch(err => {
          this.load = false
        })
    },
    // 选择分页
    handleSizeChange(size) {
      this.search.limit = size
      this.searchHandle()
    },
    handleCurrentChange(page) {
      this.search.pageNum = page
      this.searchHandle()
    },
    putData(data) {
      this.optionLoad = true
      const table = this.$refs['optionPartTable']
      const search = _.cloneDeep(this.search)
      search.partCode = data.PART_NO
      getPartOptionByPartNo(search)
        .then(res => {
          table.complete().filData(res)
          this.optionLoad = false
        })
        .catch(err => {
          this.optionLoad = false
        })

      if (data.PART_STATUS === 10011002) {
        this.$message({
          type: 'info',
          message: '该配件不可采购，可选择替代件！'
        })
      } else {
        if (data.MIN_PACKAGE != null && data.MIN_PACKAGE !== '') {
          this.form.inQuantity = data.MIN_PACKAGE
        } else {
          this.form.inQuantity = 1
        }

        this.multipleSelection = data
        this.form.partCodeShow = data.PART_NO
        this.form.partNameShow = data.PART_NAME
        if (this.orderFormObj.orderType == 60041002) {
          this.form.inPrice = data.URGENT_PRICE
        } else {
          this.form.inPrice = data.UNIT_PRICE
        }

        // this.profitQuantityChange()
      }
    },
    optionPutData(data) {
      if (data.MIN_PACKAGE != null && data.MIN_PACKAGE !== '') {
        this.form.inQuantity = data.MIN_PACKAGE
      } else {
        this.form.inQuantity = 1
      }

      this.multipleSelection = data
      this.form.partCodeShow = data.PART_NO
      this.form.partNameShow = data.PART_NAME
      if (this.orderFormObj.orderType == 60041002) {
        this.form.inPrice = data.URGENT_PRICE
      } else {
        this.form.inPrice = data.UNIT_PRICE
      }

      // this.profitQuantityChange()
    },
    // 确定
    req() {
      if (this.multipleSelection.length === 0) {
        this.$message({
          type: 'info',
          message: '请选择数据!'
        })
        return
      }
      this.done()
    },
    done() {
      /* else if (this.systemParam.minPackValid === 10041001 && (this.form.inQuantity < 0 || this.form.inQuantity < this.multipleSelection.MIN_ORDER_GOODS)) { // 最小订货量验证
       this.$message({
         type: 'warning',
         message: '选择的配件数量必须大于等于最小订货量!'
       })
     }*/

      if (this.systemParam.minPackValid === 10041001 && (this.form.inQuantity < 0 || this.form.inQuantity % this.multipleSelection.MIN_PACKAGE !== 0)) { // 最小包装数验证
        this.$message({
          type: 'warning',
          message: '选择的配件数量必须是最小包装数的倍数!'
        })
      } else {
        if (this.form.inPrice == null || this.form.inPrice === 0) {
          this.$message({
            type: 'warning',
            message: '该配件价格异常，无法采购!'
          })
          return
        }
        const data = {
          partId: this.multipleSelection.PART_ID,
          partNo: this.multipleSelection.PART_NO,
          partName: this.multipleSelection.PART_NAME,
          orderQuantity: this.form.inQuantity,
          unitPrice: this.form.inPrice,
          // priceAmount: this.form.inAmount,
          unit: this.multipleSelection.UNIT,
          minPackage: this.multipleSelection.MIN_PACKAGE
          //  minOrderGoods: this.multipleSelection.MIN_ORDER_GOODS
        }
        this.$emit('getItemInfo', data)
      }
    },
    clear() {
      this.search = {
        limit: 5,
        pageNum: 1,
        partCode: '',
        partName: '',
        havingStock: '',
        orderType: '',
        dealerCode: '',
        saleOrgCode: '',
        priceType: ''
      }
      this.form = {
        partCodeShow: '',
        partNameShow: '',
        inQuantity: '',
        inPrice: 0
        // inAmount: ''
      }
      this.multipleSelection = []
      this.partList = []
      this.$refs['optionPartTable'].clearData()

      this.dialogVisible = false
    }
  }
}

</script>

<style scoped>

</style>
